package ext.acae2.workflow;

import wt.fc.PersistenceHelper;
import wt.fc.ReferenceFactory;
import wt.util.WTException;
import wt.util.WTRuntimeException;
import wt.workflow.engine.ProcessData;
import wt.workflow.engine.WfActivity;
import wt.workflow.engine.WfBlock;
import wt.workflow.engine.WfContainer;
import wt.workflow.engine.WfProcess;
import wt.workflow.work.WorkItem;

public class TaskConfigrationHelper {
	
	public static void setActivityVariableByVarName(String oid,String variable,Object value) throws WTRuntimeException, WTException{
		ReferenceFactory rf = new ReferenceFactory();
		WorkItem wi = (WorkItem) rf.getReference(oid).getObject();
		WfActivity activity = (WfActivity) wi.getSource().getObject();
		setActivityVariableValue(activity,variable,value);
	}

	/**
	 * 设置流程变量中的参数
	 * @param process  实例流程对象
	 * @param variable 参数的名称 constraint
	 * @param value 需要设置的value值
	 * @throws WTException
	 */
	public static void setProcessVariableValue(WfProcess process,
			String variable, Object value) throws WTException {
		ProcessData procDate = process.getContext();
		procDate.setValue(variable, value);
		PersistenceHelper.manager.save(process);
	}
	
	public static void setProcessVariableByVarName(String oid,String variable,Object value) throws WTRuntimeException, WTException{
		ReferenceFactory rf = new ReferenceFactory();
		WorkItem wi = (WorkItem) rf.getReference(oid).getObject();
		WfActivity activity = (WfActivity) wi.getSource().getObject();
		WfContainer wfcont = (WfContainer) activity.getParentProcessRef().getObject();
		if (wfcont instanceof WfBlock) {
			WfBlock wb = (WfBlock)wfcont;
			wfcont = wb.getParentProcess();
		}
		WfProcess process = (WfProcess) wfcont;
		setProcessVariableValue(process,variable,value);
	}
	
	
	public static Object getProcessVariableByVarName(String oid,String variable) throws WTRuntimeException, WTException{
		ReferenceFactory rf = new ReferenceFactory();
		WorkItem wi = (WorkItem) rf.getReference(oid).getObject();
		WfActivity activity = (WfActivity) wi.getSource().getObject();
		WfContainer wfcont = (WfContainer) activity.getParentProcessRef().getObject();
		if (wfcont instanceof WfBlock) {
			WfBlock wb = (WfBlock)wfcont;
			wfcont = wb.getParentProcess();
		}
		WfProcess process = (WfProcess) wfcont;
		ProcessData pd = process.getContext();
		return pd.getValue(variable);
	}
	
	public static void setActivityVariableValue(WfActivity activity,String variable,Object value) throws WTException {
		ProcessData pd = activity.getContext();
		pd.setValue(variable, value);
		PersistenceHelper.manager.save(activity);
	}
	
	public static Object getActivityVariableByVarName(String oid,String variable ) throws WTRuntimeException, WTException{
		ReferenceFactory rf = new ReferenceFactory();
		WorkItem wi = (WorkItem)rf.getReference(oid).getObject();
		WfActivity activity = (WfActivity) wi.getSource().getObject();
		Object obj = TaskConfigrationHelper.getActivityVariableValue(activity, variable);
		return obj;
	}
	
	public static Object getActivityVariableValue(WfActivity activity,
			String variable) {
		try {
			ProcessData pd = activity.getContext();
			return pd.getValue(variable);
		} catch (Exception e) {
			return null;
		}
		
	}

}
